import React from 'react'
import PropTypes from 'prop-types'
import { routerRedux } from 'dva/router'
import { connect } from 'dva'
import { Button, Popconfirm, Row, Col } from 'antd'
import { List, Page } from 'components'
import queryString from 'query-string'
import { ListMenuType } from 'utils/enums'
import { config } from 'utils'
import styles from '../../themes/list.less'

const Entity = ({ location, dispatch, entity, loading }) => {
  location.query = queryString.parse(location.search)
  const { list, selectedRowKeys, parentId } = entity
  const listProps = {
    dataSource: list,
    menuType: ListMenuType.MENU_UPDATE + ListMenuType.MENU_DELETE,
    loading: loading.effects['entity/list'],
    location,
    columnsData: [
      {
        title: 'Name',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: 'account',
        dataIndex: 'account',
        key: 'account',
      },
      {
        title: 'accountName',
        dataIndex: 'accountName',
        key: 'accountName',
      },
    ],
    onChange (page) {
      const { query, pathname } = location
      dispatch(routerRedux.push({
        pathname,
        // query: {
        //   ...query,
        //   page: page.current,
        //   pageSize: page.pageSize,
        // },
        search: queryString.stringify({
          ...query,
          page: page.current,
          pageSize: page.pageSize,
        }),
      }))
    },
    onDeleteItem (id) {
      dispatch({
        type: 'entity/delete',
        payload: id,
      })
    },
    onEditItem (item) {
      dispatch(routerRedux.push({
        pathname: `/entity/detail/${parentId}/${item.id}`,
      }))
    },
    rowSelection: {
      selectedRowKeys,
      onChange: (keys) => {
        dispatch({
          type: 'entity/updateState',
          payload: {
            selectedRowKeys: keys,
          },
        })
      },
    },
  }
  const onAdd = () => {
    dispatch(routerRedux.push({
      pathname: `/entity/detail/${parentId}/${config.ADDFLAG}`,
    }))
  }

  return (
    <Page inner>
      <Row style={{ marginBottom: 10 }} type="flex" justify="end">
        <Col>
          <Button size="large" type="ghost" onClick={onAdd}>Create</Button>
        </Col>
      </Row>
      <List {...listProps} />
    </Page>
  )
}

Entity.propTypes = {
  entity: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func,
  loading: PropTypes.object,
}

export default connect(({ entity, loading }) => ({ entity, loading }))(Entity)
